//
//  GoodsInStorageCell.m
//  ios-supplier
//
//  Created by Benlai on 15/8/27.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
//

#import "GoodsInStorageCell.h"

@interface GoodsInStorageCell()

@property (weak, nonatomic) IBOutlet UILabel *goodsNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *gooddsCountLabel;
@property (weak, nonatomic) IBOutlet UIButton *subBtn;
@property (weak, nonatomic) IBOutlet UIButton *addBtn;

- (IBAction)subBtnPressed:(id)sender;
- (IBAction)addBtnPressed:(id)sender;

@property (nonatomic, assign) NSInteger count;
@end
@implementation GoodsInStorageCell

- (void)awakeFromNib
{ 
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    
}

-(void)setInfo:(JFBOrderProductDS *)info
{
    _info = info;
    self.goodsNameLabel.text = info.productName;
    self.gooddsCountLabel.text = [NSString stringWithFormat: @"%ld", (unsigned long)info.purchaseCount];
    self.count = info.purchaseCount;
    
    if (self.count == 0)
    {
        self.subBtn.enabled = NO;
    }
    if (self.count == self.info.purchaseCount)
    {
        self.addBtn.enabled = NO;
    }
}
- (IBAction)subBtnPressed:(id)sender
{
    if (self.count == 0)
    {
        return;
    }
    
    self.count--;
    self.info.inStorage --;
    self.gooddsCountLabel.text = [NSString stringWithFormat: @"%ld", (long)self.count];
    [self.delegate goodsInStorageChanged: self.count];
    
    if (self.count == 0)
    {
        self.subBtn.enabled = NO;
    }
    if (self.count <= self.info.purchaseCount)
    {
        self.addBtn.enabled = YES;
    }
}

- (IBAction)addBtnPressed:(id)sender
{
    if (self.count == self.info.purchaseCount)
    {
        return;
    }
    
    self.count++;
    self.info.inStorage++;
    self.gooddsCountLabel.text = [NSString stringWithFormat: @"%ld", self.count];
    [self.delegate goodsInStorageChanged: self.count];
    
    if (self.count == self.info.purchaseCount)
    {
        self.addBtn.enabled = NO;
    }
    if (self.count >= 1)
    {
        self.subBtn.enabled = YES;
    }
}
@end
